package br.ifrn.poo.aplicacao;
import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import br.ifrn.poo.controle.Conexao;
import br.ifrn.poo.usuario.Atendente;

import com.mysql.jdbc.Statement;

import javax.swing.JMenuItem;
import javax.swing.JLayeredPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;


public class Main {
	Aplicacao aplicacao = new Aplicacao();
	ArrayList<Atendente> array_atendentes = new ArrayList<Atendente>(); 
	Atendente atendente = new Atendente();
	public static JFrame frame;
	static JFrame logado;
	private JTextField login;
	private JPasswordField senha;
	Scanner entrada = new Scanner(System.in);
	public static void main(String[] args) {
		
		Conexao.getConexao();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setForeground(new Color(153, 153, 153));
		frame.setBounds(100, 100, 554, 316);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel Topo = new JPanel();
		
		JPanel panel = new JPanel();
		
		JLabel label = new JLabel("");
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(Main.class.getResource("/br/ifrn/poo/imagens/logo.png")));
		
		JLabel lblLogin = new JLabel("Login: ");
		lblLogin.setForeground(new Color(102, 102, 102));
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 22));
		
		login = new JTextField();
		login.setColumns(10);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setForeground(new Color(102, 102, 102));
		lblSenha.setFont(new Font("Tahoma", Font.PLAIN, 22));
		
		JButton btnEntrar = new JButton("Entrar");
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login();
			}
		});
		
		senha = new JPasswordField();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(btnEntrar, GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblSenha, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(senha))
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblLogin, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(login, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLogin, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
						.addComponent(login, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblSenha, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
						.addComponent(senha, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
					.addGap(22)
					.addComponent(btnEntrar, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		
		JLabel lblSistemaDeLocao = new JLabel("Sistema de Loca\u00E7\u00E3o de Ve\u00EDculos");
		lblSistemaDeLocao.setForeground(new Color(25, 25, 112));
		lblSistemaDeLocao.setHorizontalAlignment(SwingConstants.CENTER);
		lblSistemaDeLocao.setFont(new Font("Tahoma", Font.PLAIN, 22));
		GroupLayout gl_Topo = new GroupLayout(Topo);
		gl_Topo.setHorizontalGroup(
			gl_Topo.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_Topo.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSistemaDeLocao, GroupLayout.PREFERRED_SIZE, 517, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(232, Short.MAX_VALUE))
		);
		gl_Topo.setVerticalGroup(
			gl_Topo.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_Topo.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSistemaDeLocao, GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
					.addContainerGap())
		);
		Topo.setLayout(gl_Topo);
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(Topo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
					.addGap(6)
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(Topo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(6)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)))
		);
		frame.getContentPane().setLayout(groupLayout);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	public void login(){
		try {
            String url = "jdbc:mysql://localhost/locacao_veiculos";
            Connection conn = DriverManager.getConnection(url,"root","59010380");
            Statement stmt = (Statement) conn.createStatement();
            ResultSet rs;
            
            rs = stmt.executeQuery("SELECT * FROM atendente WHERE login=" +
            		"'"+login.getText()+"' AND senha='"+senha.getText()+"'");
            rs.next();

            if(login.getText().equals(rs.getString("login")) &&
            		senha.getText().equals(rs.getString("senha"))){ 
            	
            	atendente.setCpf(rs.getString("cpf"));
            	atendente.setEmail(rs.getString("email"));
            	atendente.setId_atendente(rs.getInt("idatendente"));
            	atendente.setId_carro(rs.getInt("id_carro"));
            	atendente.setLogin(rs.getString("login"));
            	atendente.setNome(rs.getString("nome"));
            	atendente.setRg(rs.getString("rg"));
            	atendente.setSenha(rs.getString("senha"));
            	array_atendentes.add(atendente);
            	
            	frame.setVisible(false);
            	frame = null;
            	aplicacao.logado(array_atendentes);
            }
            
            conn.close();
        } catch (Exception e) {
        	JOptionPane.showMessageDialog(null, "Login incorreto!!");
        }
	}
	
	
}
